package wetalk.software.bupt.com.wetalk.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import wetalk.software.bupt.com.wetalk.R;

/**
 * Created by whc07 on 2018/2/11.
 */

public class AddGroupSideBar extends View {


        private char[] l;
        private SectionIndexer sectionIndexter = null;
        private ListView list;
        private TextView mDialogText;
        private int m_nItemHeight = dipToPixel(getContext(), 15);

        public AddGroupSideBar(Context context) {
            super(context);
            init();
        }

        public AddGroupSideBar(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }

        private void init() {
            l = new char[] { '#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                    'W', 'X', 'Y', 'Z' };
        }

        public AddGroupSideBar(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }

        public void setListView(ListView _list) {
            list = _list;
        }

        public void setTextView(TextView mDialogText) {
            this.mDialogText = mDialogText;
        }

        public boolean onTouchEvent(MotionEvent event) {
            super.onTouchEvent(event);
            int i = (int) event.getY();
            int idx = i / m_nItemHeight;
            if (idx >= l.length) {
                idx = l.length - 1;
            } else if (idx < 0) {
                idx = 0;
            }
            if (event.getAction() == MotionEvent.ACTION_DOWN
                    || event.getAction() == MotionEvent.ACTION_MOVE) {
                mDialogText.setVisibility(View.VISIBLE);
                mDialogText.setText("" + l[idx]);
                if (sectionIndexter == null) {
                    HeaderViewListAdapter ha = (HeaderViewListAdapter) list
                            .getAdapter();
                    sectionIndexter = (SectionIndexer) ha.getWrappedAdapter();
                }
                int position = sectionIndexter.getPositionForSection(l[idx]);
                if (position == -1) {
                    return true;
                }
                list.setSelection(position);
            } else {
                mDialogText.setVisibility(View.INVISIBLE);
            }
            return true;
        }

        protected void onDraw(Canvas canvas) {
            Paint paint = new Paint();
            paint.setColor(getResources().getColor(R.color.gray));
            paint.setTextSize(dipToPixel(getContext(), 12));
            // paint.setTextSize(20);
            // paint.setColor(0xff595c61);
            Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
            paint.setTypeface(font);
            paint.setFlags(Paint.ANTI_ALIAS_FLAG);
            paint.setTextAlign(Paint.Align.CENTER);
            float widthCenter = getMeasuredWidth() / 2;
            for (int i = 0; i < l.length; i++) {
                canvas.drawText(String.valueOf(l[i]), widthCenter, m_nItemHeight + (i * m_nItemHeight), paint);
            }
            super.onDraw(canvas);
        }

    /**
     * DP转换为像素
     *
     * @param context
     * @param nDip
     * @return
     */

    private static float sDensity = 0;
    public static int dipToPixel(Context context, int nDip) {
        if (sDensity == 0) {
            final WindowManager wm = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(dm);
            sDensity = dm.density;
        }
        return (int) (sDensity * nDip);
    }
    }

